/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.website.service.plugins  
 */
package com.apache.website.service.plugins;

import java.util.Date;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.database.constant.Validator;
import com.apache.tools.ClassToolsUtil;
import com.apache.tools.DateUtils;
import com.apache.website.entity.Channel;
import com.apache.website.manager.ChannelManager;

/**
 * description:  保存栏目信息
 * @author dyhou 创建时间：2016-7-26  
 */
public class SaveChannelPluginImpl implements PluginConnector {

	private ChannelManager channelManager;

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo param) throws Exception {
		Channel channel = new Channel();
		channel = (Channel) ClassToolsUtil.getInstance().convert2Bean(param.getParams(), channel);
		if (Validator.isNull(channel.getParentId()))
			channel.setParentId("0");
		ParamsVo vo = new ParamsVo();
		vo.setObj(channel);
		ResultEntity entity = new ResultEntity();
		if (Validator.isNull(channel.getChannelId())) {
			channel.setCreateTime(DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
			vo.setKey("checkChannelCode");
			String flag = String.valueOf(channelManager.execute(vo));
			if ("true".equals(flag)) {
				String id = channelManager.saveInfo(vo);
				entity.setResult("true");
				entity.setEntity(id);
				entity.setMessage("保存成功");
			} else {
				entity.setMessage("栏目标识[" + channel.getCodeName() + "]已经被使用");
			}
			return entity;
		} else {
			boolean mark = channelManager.editInfo(vo);
			if (mark) {
				entity.setEntity("true");
				entity.setMessage("栏目信息修改成功！");
				return entity;
			}
		}
		entity.setMessage("操作失败！");
		return entity;
	}

	public void setChannelManager(ChannelManager channelManager) {
		this.channelManager = channelManager;
	}

}
